<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper" @click="createOrderAction(index)">
				<view class="address-box">
					<text class="address">{{item.address}} {{item.no}}</text>
				</view>
				<view class="u-box">
					<text v-if="item.isDefault === '1'" class="tag">默认</text>
					<text class="name">{{item.contact}}</text>
					<text class="mobile">{{item.tel}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji" @click.stop="addAddress('edit', item)"></text>
		</view>
		<text style="display:block;padding: 16upx 30upx 10upx;lihe-height: 1.6;color: #fa436a;font-size: 24upx;">
			温馨提示：请填写真实收货地址
		</text>

		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	import { getOpenidMsg,userAddressServicefindList } from 'api/shop';

	export default {
		data() {
			return {
				userInfo:{},
				source: 0,
				createOrder:'0',
				addressList: [
				],
				cart:'0'
			}
		},
		onLoad(option){
			console.log(window.location,'window.location.host')
			this.getOpenidMsgAction()
			console.log(option.source);
			this.source = option.source;
			if(option.createOrder == '1'){
				this.createOrder = '1'
			}
			if(option.cart == '1'){
				this.cart = '1'
			}
		},
		methods: {
			createOrderAction(index){
				console.log('createOrderAction')
				if(this.cart == '1'){
					uni.navigateTo({
						url: `/pages/order/cartOrder?index=`+index
					})
				}else{
					uni.navigateTo({
						url: `/pages/order/createOrderJf?index=`+index
					})
				}

			},
			init(cardNo){
				userAddressServicefindList({uid:cardNo}).then(ret => {
					if (ret.data.errcode === 0) {
						this.addressList = ret.data.data.result
					} else {
						this.$api.msg(ret.data.errmsg);
						return false;
					}
				});
			},
			getOpenidMsgAction() {
				getOpenidMsg().then(ret => {
					if (ret.data.errcode === 0) {
						let data = ret.data.data.result;
						if (data) {
							if (data.cardNo === null || data.cardNo === '' || data.cardNo === undefined) {
								this.handle();
							} else {
								this.userInfo = data;
								this.init(data.cardNo)
							}
						} else {
							console.log('没有此人信息');
							this.handle();
						}
					} else if (ret.data.errcode === 9999 || ret.data.errcode === 9991) {
						this.handle();
					} else {
						this.$api.msg(ret.data.errmsg);
						return false;
					}
				});
			},
			handle() {
				// window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息，这个属性可以用来判断浏览器类型
				let ua = window.navigator.userAgent.toLowerCase();
				// 通过正则表达式匹配ua中是否含有MicroMessenger字符串
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					//是微信浏览器
					window.location.href = this.$xeUtils.locat().origin + '/yq/#/handle-submit'
					// window.location.href = 'https://shengst.tripln.top/subsidy/#/city-card-handle';
				} else {
					window.location.href = 'https://shengst.tripln.top/subsidy/#/city-card-handle-app';
				}
			},
			//选择地址
			checkAddress(item){
				if(this.source == 1){
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			addAddress(type, item){
				let editId = ''
				if(type === 'edit'){
					editId = '&id='+item.id
				}
				let createOrder = ''
				if(this.createOrder == '1'){
					createOrder = '&createOrder=1'
				}
				let cart = ''
				if(this.cart == '1'){
					cart = '&cart=1'
				}
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&uid=`+this.userInfo.cardNo+editId+createOrder+cart
				})
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);

				console.log(data, type);
			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 16upx;
		.tag{
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.name{
			margin-right: 30upx;
		}
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
